package com.testing.sunnycommon.controller;

import com.testing.sunnycommon.annotations.SunnyAdvice;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.SunnyModule;
import com.testing.sunnycommon.model.SunnyProject;
import com.testing.sunnycommon.service.SunnyModuleService;
import com.testing.sunnycommon.service.SunnyProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname SunnyModuleController
 * @Description 类型说明
 * @Date 2023/4/26 22:03
 * @Created by 特斯汀Roy
 */
@RestController
@RequestMapping("/sunnyModule")
public class SunnyModuleController {
    @Autowired
    SunnyModuleService sunnyModuleService;

    @Autowired
    SunnyProjectService sunnyProjectService;

    @GetMapping
    public Result<List<SunnyModule>> listSunnyModule() {
        List<SunnyModule> list = sunnyModuleService.list();
        return Result.success(list);
    }

    @SunnyAdvice("创建新模块")
    @PostMapping
    public Result addSunnyModule(@RequestBody SunnyModule module) {
        boolean isAddOK = sunnyModuleService.save(module);
        if (isAddOK) {
            return Result.success("新增模块【" + module.getName() + "】成功", module);
        } else {
            return Result.fail(1010, "新增模块【" + module.getName() + "】失败", module);
        }
    }

    @SunnyAdvice("更新模块信息")
    @PutMapping
    public Result updateSunnyModule(@RequestBody SunnyModule module) {
        //根据传过来的实体对象的id来进行更新
        SunnyModule oldModule = sunnyModuleService.getById(module.getId());
        boolean isUpdateOK = sunnyModuleService.updateById(module);
        if (isUpdateOK) {
            return Result.success("模块【" + oldModule.getName() + "】被更名为了" + module.getName(), module);
        } else {
            return Result.fail(1020, "更新模块失败", module);
        }
    }

    @SunnyAdvice("删除模块")
    @DeleteMapping("/{ids}")
    public Result deleteSunnyModules(@PathVariable List<Long> ids) {
        List<SunnyModule> moduleList = sunnyModuleService.listByIds(ids);
        boolean isDeleteOK = sunnyModuleService.removeBatchByIds(ids);
        //获取所有的项目，删除关联的模块
        List<SunnyProject> projectList = sunnyProjectService.list();
        //遍历所有的项目，删除其中modulelist字符串的在ids里的id,重新统计数量并且更新数据库
        projectList.forEach(sunnyProject -> {
            String moduleListString = sunnyProject.getModuleList();
            //需要替换的是三种情况： 开头1，  ，1， 换成,   ,1结束
            for (Long moduleId : ids) {
                moduleListString = moduleListString.replaceAll("," + moduleId + ",", ",").replaceAll("^" + moduleId + ",", "").replaceAll("," + moduleId + "$", "");
            }
            //重设为替换完之后的字符串
            sunnyProject.setModuleList(moduleListString);
            //重新更新一下项目，触发用例数的统计
            sunnyProjectService.updateSunnyProject(sunnyProject);
        });

        if (isDeleteOK) {
            return Result.success("删除了模块【" + moduleList
                    .stream().map(SunnyModule::getName)
                    .collect(Collectors.toList()) + "】，并且从各个项目中移除了它们", moduleList);
        } else {
            return Result.fail(1030, "删除模块失败", "失败的模块是" + moduleList);
        }
    }

}
